package com.baizhi.action.front;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.baizhi.domain.Result;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UserServiceImpl;
import com.baizhi.utils.SendEmailUtil;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.UUID;

public class UserAction {
    private User user;
    private String username;
    private String password;
    private String errorMsg;
    private String email;
    private String code;
    private String validateCode;

    public String getValidateCode() {
        return validateCode;
    }

    public void setValidateCode(String validateCode) {
        this.validateCode = validateCode;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String login() throws UnsupportedEncodingException {
        UserService userService=new UserServiceImpl();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Result result = userService.login(username, password);
        if (result.isFlag()){
            session.setAttribute("user",result.getData());
            return "loginSuccess";
        }else {
            errorMsg= URLEncoder.encode( result.getMessage(),"utf-8");
            return "loginError";
        }
    }

    public String register() throws UnsupportedEncodingException {
        UserService userService=new UserServiceImpl();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        //生成盐
        String salt=RandomUtil.randomString(6);
        //生成激活码
        String code=RandomUtil.randomString(6);
        HMac md5 = new HMac(HmacAlgorithm.HmacMD5,salt.getBytes());
        String uuid= UUID.randomUUID().toString();
        user.setUserId(uuid);
        user.setSalt(salt);
        user.setCode(code);
        user.setStatus("禁用");
        user.setPassword(md5.digestHex(user.getPassword()));
        user.setCreateDate(new Date());
        String realCode= (String) session.getAttribute("realCode");
        if(!realCode.equalsIgnoreCase(validateCode)){
            errorMsg= URLEncoder.encode("验证码错误","utf-8");
            return "registerError";
        }
        Boolean flag = userService.register(user);
        if (flag){
            SendEmailUtil.sendCode(user.getEmail(),user.getCode());
            return "registerSuccess";
        }else {
            errorMsg= URLEncoder.encode("你已经注册过了...不能重复注册","utf-8");
            return "registerError";
        }
    }

    public String verify() throws UnsupportedEncodingException {
        UserService userService=new UserServiceImpl();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();

        user = userService.selectByEmail(email);
        if (user!=null){
            if (user.getCode().equals(code)){
                user.setStatus("正常");
                userService.updateUserStatus(user);
                session.setAttribute("user",user);
                return "verifySuccess";
            }else {
                errorMsg= URLEncoder.encode("激活码不正确","utf-8");
                return "verifyError";
            }
        }else {
            errorMsg= URLEncoder.encode("用户不存在","utf-8");
            return "verifyError";
        }
    }

    public String logout(){
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpSession session=req.getSession();
        //session.invalidate();
        session.removeAttribute("user");
        return "logoutSuccess";
    }
}
